using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using ZO.SmartCore.Interop.Windows;

namespace ZO.SmartCore.Windows.Forms
{
    /// <summary>
    /// provides Enhanced MessageBox Support
    /// </summary>
    [ToolboxBitmap(typeof(SmartMessageBoxManager), "SmartCore.Windows.Forms.SmartMessageBoxManager.bmp")]
    [ToolboxItem(true)]
    public class SmartMessageBoxManager : SmartComponent
    {
 
        /// <summary>
        /// Gets or sets a value indicating whether the message-box is centered on its parent.
        /// </summary>
        /// <value><c>true</c> if the message-box is centered on its parent; otherwise, <c>false</c>.</value>
        public bool CenterWindow
        {
            get
            {
                return _CenterWindow;
            }
            set
            {
                _CenterWindow = value;
            }
        }

        
        
        private bool _CenterWindow;

        private WindowHook hook = new WindowHook();

        private bool IsEventsAttched;
        private bool _Enabled;

        /// <summary>
        /// Initializes a new instance of the <see cref="SmartMessageBoxManager"/> class.
        /// </summary>
        public SmartMessageBoxManager()
            : base()
        {
            IsEventsAttched = false;
        }

  


        /// <summary>
        /// Initializes a new instance of the <see cref="SmartMessageBoxManager"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="parentContainer">The parent container.</param>
        public SmartMessageBoxManager(IContainer container, ContainerControl parentContainer)
            :
            base(container, parentContainer)
        {

            if (!DesignMode)
            {
                Form form = ContainerControl as Form;

                if (form != null)
                {
                    form.Load += new EventHandler(form_Load);

                    IsEventsAttched = true;
                }
                else
                {
                    UserControl control = ContainerControl as UserControl;

                    if (control != null)
                    {
                        control.Load += new EventHandler(control_Load);

                        IsEventsAttched = true;

                    }
                } 
            }

        }




        /// <summary>
        /// Handles the Load event of the control control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void control_Load(object sender, EventArgs e)
        {
            if (Enabled)
            {
                this.EnabledInternal = true;
            }

        }


        /// <summary>
        /// Handles the Load event of the form control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        private void form_Load(object sender, EventArgs e)
        {
            if (Enabled)
            {
                this.EnabledInternal = true;
            }
        }

     
  


        /// <summary>
        /// Initializes a new instance of the <see cref="SmartMessageBoxManager"/> class.
        /// </summary>
        /// <param name="container">The container.</param>
        public SmartMessageBoxManager(IContainer container)
            :
            base(container) { IsEventsAttched = false; }


        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="SmartMessageBoxManager"/> is enabled.
        /// </summary>
        /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
        private bool EnabledInternal
        {
            set
            {
                if (hook.Installed != value)
                {
                    if (value)
                    {

                        if (hook.Installed)
                        {
                            hook.Activating -= new EventHandler<WindowActivatingEventArgs>(hook_Activating);
                        }
                        if (!DesignMode)
                        {
                            hook.Install();
                            hook.Activating += new EventHandler<WindowActivatingEventArgs>(hook_Activating);
                        }
                    }
                    else
                    {

                        if (hook.Installed)
                        {
                            hook.Activating -= new EventHandler<WindowActivatingEventArgs>(hook_Activating);
                            hook.Uninstall();
                        }

                    }
                }
            }
        }

        /// <summary>
        /// Handles the Activating event of the hook control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:SmartCore.Interop.Windows.WindowActivatingEventArgs"/> instance containing the event data.</param>
        private void hook_Activating(object sender, WindowActivatingEventArgs e)
        {
            if (e.Window.IsMessageBox)
            {
                CentertheWindow(e.Window, e.Window.Parent);
            }
        }


        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="SmartMessageBoxManager"/> is enabled.
        /// </summary>
        /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
        public bool Enabled
        {
            get
            {
                return _Enabled;
            }
            set
            {
                if (_Enabled != value)
                {
                    _Enabled = value;

                    if (!DesignMode)
                    {
                        this.EnabledInternal = _Enabled;
                    }

                }
            }
        }
    

        /// <summary>
        /// Centerthes the window.
        /// </summary>
        /// <param name="child">The child.</param>
        /// <param name="parent">The parent.</param>
        private static void CentertheWindow(Window child, Window parent)
        {
            //if parent doesn't exists
            if (!parent.Exists)
            {
                //don't centre the msgbox
                return;
            }

            Rectangle parentRect;
            Rectangle childRect;

            childRect = child.WindowRect;
            parentRect = parent.WindowRect;

            int widthChild = childRect.Width;
            int heightChild = childRect.Height;
            int left = parentRect.Left + (parentRect.Width - widthChild) / 2;
            int top = parentRect.Top + (parentRect.Height - heightChild) / 2;

            child.Move(left, top, widthChild, heightChild);	

        }


        /// <summary>
        /// Override This Method To Dispose Unmanaged Resources.
        /// </summary>
        protected override void DisposeManagedResources()
        {
            if (this.hook != null)
            {
                this.hook.Dispose();
                this.hook = null;
            }

            if (IsEventsAttched)
            {
                Form form = ContainerControl as Form;

                if (form != null)
                {
                    form.Load -= new EventHandler(form_Load);

                    IsEventsAttched = false;
                }
                else
                {
                    UserControl control = ContainerControl as UserControl;

                    if (control != null)
                    {
                        control.Load -= new EventHandler(control_Load);

                        IsEventsAttched = false;

                    }
                }


            }

            base.DisposeManagedResources();
        }

   
   

    
     
    }
}
